import { ApiBase } from '@/types/api/apiBase';

import service from '@/utils/http';
import { Group } from '@/types/group';
import { Miner } from '@/types/miner';
import formatMinerCount from '@/utils/monitor/formatMinerCount';
import { CoinInfoSeriesData } from '@/types/AnalysisInfo';
import formatHash from '@/utils/format/formatHash';
import currencyListModel from '@/models/currencylist';

function getTotalGroup(mid: number, groupList: Group[]) {
  let normal_count = 0,
    warning_count = 0,
    abnormal_count = 0,
    downtime_count = 0,
    online_count = 0,
    offline_count = 0;
  groupList.forEach((group) => {
    if (group.gid && group.miner_count) {
      normal_count += group.miner_count.normal_count;
      warning_count += group.miner_count.warning_count;
      abnormal_count += group.miner_count.abnormal_count;
      downtime_count += group.miner_count.downtime_count;
      online_count += group.miner_count.online_count;
      offline_count += group.miner_count.offline_count;
    }
  });
  return {
    checkGpuHashWarning: true,
    ctime: 1603855100,
    gid: 0,
    gpuErrorHash: 25,
    gpuWaringHash: 20,
    hash: 0,
    hottest: '',
    main_coin: '',
    main_wallet: '',
    mid: mid,
    miner: '',
    miner_num: online_count + offline_count,
    mtime: 0,
    name: '全部分组',
    os: '',
    pool: '',
    pool_desc: '',
    sc_hash: 0,
    second_coin: '',
    second_wallet: '',
    username: '',
    miner_count: {
      normal_count,
      warning_count,
      abnormal_count,
      downtime_count,
      online_count,
      offline_count,
    },
  };
}

export function fetchOriginGroup(mid: number) {
  return new Promise<Group[]>(async (resolve, reject) => {
    const res = await service(`/web/group`, {
      method: 'GET',
      params: {
        mid,
      },
    });
    if (res.data.code === 0) {
      resolve(res.data.result.list);
    } else {
      reject();
    }
  });
}

export interface GroupPageModel {
  groupList: Group[];
  currentGroup: Group | undefined;
  groupListMap: Record<string, Group>;
  groupCount: number;
  coinInfoChartLegendData: string[];
  coinInfoChartSeriesData: CoinInfoSeriesData[];
}

export function fetchGroup(url: string, mid: number, gid: number | undefined) {
  return new Promise<GroupPageModel>(async (resolve) => {
    let groupList = await fetchOriginGroup(mid);
    const currencyMap = await currencyListModel.getCurrencyMap();
    const coinInfoChartLegendDataMap: Record<string, CoinInfoSeriesData> = {};
    groupList = groupList.map((group) => {
      if (!coinInfoChartLegendDataMap[group.main_coin]) {
        coinInfoChartLegendDataMap[group.main_coin] = {
          name: group.main_coin,
          value: group.hash,
          hashFormat: '',
        };
      } else {
        coinInfoChartLegendDataMap[group.main_coin].value += group.hash;
      }
      return {
        ...group,
        miner_count: formatMinerCount(group.miner, group.miner_num),
        hashFormat: formatHash(
          group.hash,
          group.main_coin,
          currencyMap.get(group.main_coin),
        ),
      };
    });

    const groupListMap = groupList.reduce<Record<string, Group>>(
      (memo, cur) => {
        memo[cur.gid.toString()] = cur;
        return memo;
      },
      {},
    );
    let currentGroup;
    if (gid) {
      currentGroup = groupList.find((group) => group.gid === gid);
    } else {
      currentGroup = getTotalGroup(mid, groupList);
    }

    const coinInfoChartLegendData = Object.keys(coinInfoChartLegendDataMap);

    const coinInfoChartSeriesData: CoinInfoSeriesData[] =
      coinInfoChartLegendData.map((l) => {
        return {
          name: l,
          value: coinInfoChartLegendDataMap[l].value,
          hashFormat: formatHash(
            coinInfoChartLegendDataMap[l].value,
            l,
            currencyMap.get(l),
          ),
        };
      });

    resolve({
      currentGroup,
      groupListMap,
      groupList,
      groupCount: groupList.length,
      coinInfoChartLegendData,
      coinInfoChartSeriesData,
    });
  });
}
